let fetch = require("../../utils/fetch");
let mta = require('../../utils/mta_analysis.js');

let app = getApp();
Page({
	data: {
		num: '85',
		imageUrl: app.globalData.imageUrl,
		shopName: '请选择门店',
		mxPrice: "0",
		memberLevel: getApp().globalData.userInfo.level,
		shopAddress: '',
		urlAddress: getApp().globalData.imageUrl
	},

	onLoad: function(options) {
		var _self = this;
		this.setData({
			memberLevel: _self.vip_Level(getApp().globalData.userInfo.level)
		})
		// console.log(getApp().globalData.userInfo.level)
	},

	onReady: function() {
		console.log('onReady')
	},
	onShow: function() {
		console.log("*****")
		console.log(getApp().globalData.common.mxShopCode)
		if (getApp().globalData.common.mxShopCode) {

		} else {
			getApp().globalData.common.mxShopCode = getApp().globalData.common.shopCode;
		}
		this.getPrice();


	},
	onHide: function() {},
	onUnload: function() {

	},
	getPrice: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/maixiang/setting/level.do',
			data: {
				shopCode: getApp().globalData.common.mxShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					_self.setData({
						mxPrice: res.data.obj.price,
						memberLevel: _self.vip_Level(res.data.obj.levelCode),
						discountMax: res.data.obj.discountMax,
                        discountRatio:res.data.obj.discountRatio
 

					})
					_self.getShopName(res.data.obj.shopCode);


				} else {

				}



			},
			fail: function() {
				console.log(1)
			}
		})
	},
	submitAction: function() {
		if (!getApp().globalData.common.mxShopCode) {
			return;
		}
		var _self = this;
		wx.showModal({
			title: '购买的麦享会员门店为：' + _self.data.shopName,
			content: _self.data.shopAddress,
			success: function(res) {
				if (res.confirm) {
					_self.mxPayAction();
					console.log('用户点击确定')
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})

	},
	getShopName: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/shop/getShop.do',
			data: {
				shopCode: getApp().globalData.common.mxShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					if (res.data.obj.address.length > 40) {
						res.data.obj.address = res.data.obj.address.substring(0, 40) + '…'
					}
					_self.setData({
						shopName: res.data.obj.shopName,
						shopAddress: res.data.obj.address
					});
				} else {
					_self.setData({
						shopName: '请选择门店'
					});
				}
			}
		})
	},
	mxPayAction: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/maixiang/orderAndPay.do?shopCode=' + getApp().globalData.common.mxShopCode,
			method: "POST",

			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					console.log(res)
					wx.requestPayment({
							'timeStamp': res.data.obj.timeStamp,
							'nonceStr': res.data.obj.nonceStr,
							'package': res.data.obj.prepay_id,
							'signType': res.data.obj.signType,
							'paySign': res.data.obj.paySign,
							'success': function(response) {
								fetch.showToast({
									title: '支付成功',
									icon: 'success',
									duration: 2000,
									mask: true
								})
								app.globalData.common.selfPay = 1
								// wx.switchTab({     url:'../mine/mine' })
								wx.switchTab({
									url: '../mine/mine'
								})
							},
							'fail': function(res) {
								console.log("支付失败")

							}
						} // 支付方式为余额支付时
					)

				} else {
					_self.showMyToast(_self, res.data.des)
				}
			}
		})
	},
	vip_Level: function(level) {
		switch (level) {
			case "00":
				return "注册会员"

				break;
			case "01":

				return "青铜会员"

				break;
			case "02":

				return "白银会员"

				break;
			case "03":

				return "黄金会员"

				break;
			case "04":

				return "钻石会员"

				break;
			default:

				vipLevel: "注册会员"

		}
	},
	showMyToast: function(self, my_toast_tip, time = 1000, template = 'my_toast') {
		self.setData({
			template: template, //my_toast
			my_toast_tip: my_toast_tip
		})
		var timer = setTimeout(() => {
			self.setData({
				template: '', //my_toast
				my_toast_tip: ''
			})
			clearTimeout(timer);
		}, time)

	}


})